﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Sinacor.Infra.Core.Licensing.Business;


namespace WebLicenseGenerator.UC
{
	public enum AliasInputResult
	{
		Save = 1,
		Cancel = 2
	}

	public class AliasInputEventArgs
	{
		AliasInputResult _result;
		public AliasInputResult Result
		{
			get { return _result; }
			set { _result = value; }
		}

		public AliasInputEventArgs(AliasInputResult result)
		{
			_result = result;
		}
	}

	public partial class AliasInput : System.Web.UI.UserControl
	{
		Alias _aliasBusiness;
		public event AliasInputEventHandler OnAction;
		public delegate void AliasInputEventHandler(object sender, AliasInputEventArgs e);

		public string Title
		{
			get { return lblInputTitle.Text; }
			set { lblInputTitle.Text = value.ToUpper(); }
		}
		
		public string Message
		{
			get { return lblInputMessage.Text; }
			set { lblInputMessage.Text = value; }
		}

		public int AliasId
		{
			get
			{
				if (ViewState["AliasInputAliasId"] != null)
					return Convert.ToInt32(ViewState["AliasInputAliasId"]);
				else
					return 0;
			}
			set { ViewState["AliasInputAliasId"] = value; }
		}

		public string AliasName
		{
			get { return txtAliasName.Text; }
			set { txtAliasName.Text = value; }
		}

        protected void Page_Load(object sender, EventArgs e)
        {
		}

		protected void btOk_Click(object sender, EventArgs e)
		{
			if (this.ValidateFields())
			{
				Sinacor.Infra.Core.Licensing.Alias alias = new Sinacor.Infra.Core.Licensing.Alias();
				alias.ID = this.AliasId;
				alias.Nome = txtAliasName.Text.Trim();

				_aliasBusiness = new Alias();
				_aliasBusiness.Alterar(alias);

				if (OnAction != null)
					OnAction(this, new AliasInputEventArgs(AliasInputResult.Save));

				this.HideInputDialog();
			}
			else
				this.ShowInputDialog();
		}

		protected void btCancel_Click(object sender, EventArgs e)
		{
			if (OnAction != null)
				OnAction(this, new AliasInputEventArgs(AliasInputResult.Cancel));

			this.HideInputDialog();
		}

		public void ShowInputDialog()
		{
			mpe.Show();
		}

		public void HideInputDialog()
		{
			mpe.Hide();
		}

		bool ValidateFields()
		{
			return (txtAliasName.IsValid);
		}
	}
}